<template>
  <div class="app-container">
    <!-- 条件筛选组件 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="标签名称:">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item label="分类名称:">
        <el-select v-model="query.categoryId" clearable filterable>
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="openAdd"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 列表组件 -->
    <el-table :data="list" stripe border style="width: 100%">
      <el-table-column align="center" label="序号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" prop="name" label="标签名称">
      </el-table-column>
      <el-table-column align="center" prop="categoryName" label="分类名称">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>

    <edit
      :title="edit.title"
      :visible="edit.visible"
      :formData="edit.formData"
      :categoryList="categoryList"
      :remoteClose="remoteClose"
    />
  </div>
</template>

<script>
import api from "@/api/label";
import Edit from "@/views/label/edit.vue";
export default {
  data() {
    return {
      list: [],
      page: {
        current: 1,
        size: 20,
        total: 0,
      }, //分页
      query: {},
      categoryList: [], //正常状态的分类
      edit: {
        title: "",
        visible: false,
        formData: {},
      },
    };
  },
  created() {
    this.fetchData();
    this.getCategoryList();
  },
  components: {
    Edit,
  },

  methods: {
    fetchData() {
      api.getList(this.query, this.page.current, this.page.size).then((res) => {
        this.list = res.data.records;
        this.page.total = res.data.total;
      });
    },
    handleEdit(id) {
      api.getById(id).then((res) => {
        if (res.code == 20000) {
          this.edit.formData = res.data;
          this.edit.visible = true;
          this.edit.title = "编辑";
        }
      });
    },
    handleDelete(id) {
      this.$confirm("确认删除这条记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          api.deleteById(id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
          });
          this.fetchData();
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    handleCurrentChange() {
      this.page.current = val;
      this.fetchData();
    },
    getCategoryList() {
      api.getNormalList().then((res) => {
        this.categoryList = res.data;
        console.log(this.categoryList);
      });
    },
    queryData() {
      this.page.current = 1;
      this.fetchData();
    },
    reload() {
      this.query = {};
      this.fetchData();
    },
    openAdd() {
      this.getCategoryList();
      this.edit.visible = true;
      this.edit.title = "新增";
    },
    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },
  },
};
</script>

<style scoped>
</style>